﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Net.Mail;
using System.Text;

namespace Forex.IB.RealTimeBars.Components
{
    public class SendEmail
    {
        public SendEmail()
        {

        }

        public void Notify(Stream reportStream, string reportName, string[] recipients, string[] cc, string[] bcc, string subject, string body, string smtpServer)
        {
            // todo: Get notification template from config
            Attachment attachment = null;
            string fileName = string.Format("{0}_{1}_{2}.csv", reportName, DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("HHmmss"));
            System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
            contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
            contentType.Name = fileName;
            if (reportStream != null)
            {
                attachment = new Attachment(reportStream, contentType);
            }

            // ensure that recipients are , separated
            string toList = FormatEmailList(recipients);
            string ccList = FormatEmailList(cc);
            string bccList = FormatEmailList(bcc);

            Email(toList, ccList, bccList, Common.Sender, subject, body, attachment, false, smtpServer);

        }



        public void Notify(Stream reportStream, string reportName, string[] recipients, string[] cc, string[] bcc, string subject, string body, string smtpServer, bool isHtml)
        {
            // todo: Get notification template from config
            Attachment attachment = null;
            string fileName = string.Format("{0}_{1}_{2}.csv", reportName, DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("HHmmss"));
            System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
            contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
            contentType.Name = fileName;
            if (reportStream != null)
            {
                attachment = new Attachment(reportStream, contentType);
            }

            // ensure that recipients are , separated
            string toList = FormatEmailList(recipients);
            string ccList = FormatEmailList(cc);
            string bccList = FormatEmailList(bcc);

            Email(toList, ccList, bccList, Common.Sender, subject, body, attachment, isHtml, smtpServer);

        }

        public void TradeNotify(string[] recipients, string[] cc, string[] bcc, string subject, string body, string smtpServer)
        {
            // ensure that recipients are , separated
            string toList = FormatEmailList(recipients);
            string ccList = FormatEmailList(cc);
            string bccList = FormatEmailList(bcc);

            Email(toList, ccList, bccList, Common.Sender, subject, body, null, false, smtpServer);

        }
        private void Email(string recipients, string cc, string bcc, string sender, string subject, string body, System.Net.Mail.Attachment attachment, bool isHtml, string smtpServer)
        {
            // validation
            if (string.IsNullOrEmpty(recipients)) throw new ArgumentException("recipients");
            if (string.IsNullOrEmpty(sender)) throw new ArgumentException("sender");
            if (string.IsNullOrEmpty(subject)) throw new ArgumentException("subject");

            MailMessage message = new MailMessage();
            var client = new SmtpClient(Common.smtpclient, 587)
            {
                Credentials = new NetworkCredential(Common.smtpUserName, Common.smtpPassword),
                EnableSsl = true
            };
           
            message.To.Add(recipients);
            if (!string.IsNullOrEmpty(bcc)) message.Bcc.Add(bcc);
            if (!string.IsNullOrEmpty(cc)) message.CC.Add(cc);
            message.From = new MailAddress(sender);
            message.Subject = subject;

            if (!string.IsNullOrEmpty(body)) message.Body = body;

            if (isHtml)
            {
                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.GetEncoding(65001); // RFC1489
            }

            if (attachment != null) message.Attachments.Add(attachment);

            if (string.IsNullOrEmpty(smtpServer)) smtpServer = "127.0.0.1";

            client.Host = smtpServer;

            client.Send(message);            
        }


        private string FormatEmailList(string[] addresses)
        {
            // leave as null if nothing is sent
            if (addresses == null) return null;

            string list = null;
            string comma = "";
            foreach (string address in addresses)
            {
                list += comma + address;
                comma = ", ";
            }

            return list;
        }
    }
}
